// import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import LoginPage from '@/views/LoginPage'
import UserPage from '@/views/UserPage'
import { getToken, getPerm } from '@/request/token'


function view (path) {
	return function (resolve) {
		import(`@/views/${path}.vue`).then(mod => {
			resolve(mod)
		})
	}
}

const router = new Router({
	mode: 'history',
	routes: [
		{
			path: '/WordPage',
			name: 'WordPage',
			component: view('WordPage')
		},
		{
			path: '/CloseList',
			name: 'CloseList',
			component: view('CloseList')
		},
		{
			path: '/ClosePage/:id',
			name: 'ClosePage',
			component: view('ClosePage')
		},
		{
			path: '/ManagePage',
			name: 'ManagePage',
			component: view('ManagePage'),
			children: [
				{
					path: '/ManagePage/QuForm/QuNew',
					name: 'QuNew',
					component: view('qu/form')
				},
				{
					path: '/ManagePage/QuForm/:id',
					name: 'QuForm',
					component: view('qu/form')
				},
				{
					path: '/ManagePage/Questions',
					name: 'Questions',
					component: view('qu/index')
				},
				
				{
					path: '/ManagePage/SubjectForm/SubjectNew',
					name: 'SubjectNew',
					component: view('subject/form')
				},
				{
					path: '/ManagePage/SubjectForm/:id',
					name: 'SubjectForm',
					component: view('subject/form')
				},
				{
					path: '/ManagePage/Subjects',
					name: 'Subjects',
					component: view('subject/index')
				},
				{
					path: '/ManagePage/Subjects/Paper/:id',
					name: 'SubjectPaper',
					component: view('paper/form')
				},
				{
					path: '/ManagePage/Exams',
					name: 'Exams',
					component: view('exam/index')
				},
				{
					path: '/ManagePage/ExamHistory',
					name: 'ExamHistory',
					component: view('exam/user')
				},
				{
					path: '/ManagePage/ExamProcess/:id',
					name: 'ExamProcess',
					component: view('exam/exam')
				},
				{
					path: '/ManagePage/ExamResult',
					name: 'ExamResult',
					component: view('exam/result')
				},
			]
		},
		{
			path: '',
			name: 'Home',
			component: HelloWorld
		},
		{
			path: '/login',
			name: 'Login',
			component: LoginPage
		},
		{
			path: '/UserPage',
			name: 'User',
			component: UserPage
		},
	]
})

const whiteList = ['/login']
// 导航守卫，渲染动态路由
router.beforeEach((to, from, next) => {
	if (whiteList.indexOf(to.path) !== -1 || getToken()) 
		next()
	else
		next('/login')
})

export default router